<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ page import="com.cdvtc.fengchao.dao.UsersDAO" %>
<%@ page import="com.cdvtc.fengchao.model.User" %>
<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>用户注册 - 绿蜂巢</title>
    <link rel="stylesheet" href="static/css/styles.css">
    <style>
        /* 保持原有样式不变 */
        .register-container {
            min-height: calc(100vh - 80px - 50px);
            display: flex;
            justify-content: center;
            align-items: center;
        }
        /* 其他样式... */
    </style>
</head>
<body>
<jsp:include page="header.jsp" />

<main class="register-container">
    <form class="register-form"
          action="<%=request.getContextPath()%>/signup.jsp"
          method="post"
          accept-charset="UTF-8">
        <!-- 表单字段 -->
        <div class="form-group">
            <label class="form-label" for="username">用户名</label>
            <input type="text" class="form-input" id="username" name="username" required>
        </div>
        <div class="form-group">
            <label class="form-label" for="phone">联系电话</label>
            <input type="tel" class="form-input" id="phone" name="phone" pattern="[0-9]{11}" required>
        </div>
        <div class="form-group">
            <label class="form-label" for="address">联系地址</label>
            <input type="text" class="form-input" id="address" name="address" required>
        </div>
        <div class="form-group">
            <label class="form-label" for="email">邮箱</label>
            <input type="email" class="form-input" id="email" name="email" required>
        </div>
        <div class="form-group">
            <label class="form-label" for="password">密码</label>
            <input type="password" class="form-input" id="password" name="password" required>
            <div class="error-message" id="passwordError"></div>
        </div>
        <button type="submit" class="register-btn">立即注册</button>
    </form>
</main>

<jsp:include page="footer.jsp" />

<%
    // 设置请求编码为UTF-8
    request.setCharacterEncoding("UTF-8");

    // 处理注册逻辑
    if ("POST".equals(request.getMethod())) {
        // 1. 获取表单数据
        String username = request.getParameter("username");
        String phone = request.getParameter("phone");
        String address = request.getParameter("address");
        String email = request.getParameter("email");
        String password = request.getParameter("password");

        // 2. 验证数据
        boolean isValid = true;
        if (username == null || username.trim().isEmpty()) {
            isValid = false;
%>
<script>alert('用户名不能为空');</script>
<%
    }
    if (password == null || password.length() < 6) {
        isValid = false;
%>
<script>alert('密码长度至少为6个字符');</script>
<%
    }
    if (!isValid) {
        return; // 终止脚本执行
    }

    // 3. 调用DAO注册用户
    User user = new User();
    user.setUsername(username);
    user.setPhone(phone);
    user.setAddress(address);
    user.setEmail(email);
    user.setPassword(password);

    UsersDAO dao = new UsersDAO();
    boolean registerSuccess = dao.registerUser(user);

    // 4. 返回注册结果
    if (registerSuccess) {
%>
<script>
    window.location.href = 'login.jsp';
</script>
<%
} else {
%>
<script>alert('注册失败，请稍后再试');</script>
<%
        }
    }
%>
</body>
</html>